package com.baidu.acg.industry.bigdata.common;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.nio.charset.StandardCharsets;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.CONFLICT;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.HttpStatus.NO_CONTENT;
import static org.springframework.http.HttpStatus.OK;

/**
 * CommonResponse
 *
 * @author xuyanpeng01 on 2022/5/19
 */
public class CommonResponse {

    public static <T> ResponseEntity<T> ok(T obj) {
        return patternResponse(OK, obj);
    }

//    public static <T> ResponseEntity<String> create(T obj) {
//        return response(CREATED, obj);
//    }

//    public static <T> ResponseEntity<String> noContent() {
//        return response(NO_CONTENT, null);
//    }
//
//    public static <T> ResponseEntity<String> badRequest(Exception ex) {
//        return response(BAD_REQUEST, new Exception(ex));
//    }
//
//    public static <T> ResponseEntity<String> notFound(Exception ex) {
//        return response(NOT_FOUND, new Exception(ex));
//    }
//
//    public static ResponseEntity<String> conflict(Exception ex) {
//        return response(CONFLICT, new Exception(ex));
//    }
//
//    public static ResponseEntity<String> internalError(Exception ex) {
//        return response(INTERNAL_SERVER_ERROR, new Exception(ex));
//    }

//    private static <T> ResponseEntity<String> response(HttpStatus status, T obj) {
//        ObjectMapper mapper = new ObjectMapper();
//        mapper.registerModule(new JavaTimeModule());
//        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
//        mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true));
//        try {
//            MediaType mediaType = new MediaType("application", "json", StandardCharsets.UTF_8);
//            return ResponseEntity.status(status)
//                .contentType(mediaType)
//                .body(mapper.writeValueAsString(obj));
//        } catch (JsonProcessingException ex) {
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.toString());
//        }
//    }

    private static <T> ResponseEntity<T> patternResponse(HttpStatus status, T obj) {
        MediaType mediaType = new MediaType("application", "json", StandardCharsets.UTF_8);
        return ResponseEntity.status(status)
            .contentType(mediaType)
            .body(obj);
    }

}
